#pragma once 
#include <string>

#include "server/rtmp/amf0/amf0_inc.hpp"
#include "server/rtmp/rtmp_protocol/rtmp_define.hpp"
#include "server/rtmp/rtmp_message/rtmp_message.hpp"

#include "rtmp_connect_command_message.hpp"

namespace mms {

class RtmpConnectRespMessage{
public:
    RtmpConnectRespMessage(const RtmpConnectCommandMessage& conn_msg,
        const std::string& name){

            command_name_.set_value(name);
            transaction_id_.set_value(conn_msg.transaction_id_.get_value());
        }

    RtmpConnectRespMessage(){

    }

    virtual ~RtmpConnectRespMessage() {}

public:
    int32_t decode(std::shared_ptr<RtmpMessage> rtmp_msg){
        const uint8_t* payload = rtmp_msg->payload_;
        const uint8_t* data_start = payload;
        int32_t len = rtmp_msg->payload_size_;
        int32_t consumed = command_name_.decode(payload,len);
        if(consumed <= 0){
            return -1;
        }

        payload += consumed;
        len -= consumed;

        consumed = transaction_id_.decode(payload,len);
        if(consumed <= 0){
            return -2;
        }
        payload += consumed;
        len -= consumed;

        consumed = props_.decode(payload,len);
        if(consumed <= 0){
            return -3;
        }
        payload += consumed;
        len -= consumed;

        consumed = info_.decode(payload,len);
        if(consumed <= 0){
            return -4;
        }
        payload += consumed;
        len -= consumed;

        return payload - data_start;

    }


    std::shared_ptr<RtmpMessage> encode() {
        size_t s = 0;
        s += command_name_.size();
        s += transaction_id_.size();
        s += props_.size();
        s += info_.size();

        std::shared_ptr<RtmpMessage> rtmp_msg = std::make_shared<RtmpMessage>(s);
        rtmp_msg->chunk_stream_id_ = RTMP_CHUNK_ID_PROTOCOL_CONTROL_MESSAGE;
        rtmp_msg->timestamp_ = 0;
        rtmp_msg->message_strean_id_ = RTMP_MESSAGE_ID_PROTOCOL_CONTROL;
        rtmp_msg->message_type_id_ = RTMP_MESSAGE_TYPE_AMF0_COMMAND;
        uint8_t* payload = rtmp_msg->payload_;
        int32_t len = s;
        int32_t consumed = command_name_.encode(payload,len);
        if(consumed < 0){
            return nullptr;
        }
        payload += consumed;
        len -= consumed;

        consumed = transaction_id_.encode(payload,len);
        if(consumed < 0){
            return nullptr;
        }
        payload += consumed;
        len -= consumed;

        consumed = props_.encode(payload,len);
        if(consumed < 0){
            return nullptr;
        }
        payload += consumed;
        len -= consumed;

        consumed = info_.encode(payload,len);
        if(consumed < 0){
            return nullptr;
        }
        payload += consumed;
        len -= consumed;
        rtmp_msg->payload_size_ = s;
        return rtmp_msg;
    }

    Amf0Object& props(){
        return props_;
    }

    Amf0Object& info(){
        return info_;
    }


public:
    Amf0String command_name_;
    Amf0Number transaction_id_;
    Amf0Object props_;
    Amf0Object info_;

};


}